<template>
  <div class="container">
    <div class="header">
      <el-input
        @keyup.enter.native="init"
        v-model="input"
        placeholder="请输入内容"
        clearable
      >
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="init"
        ></i>
      </el-input>
      <el-button icon="el-icon-plus" @click="add" class="btn"
        >添加员工</el-button
      >
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="员工姓名"> </el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="status" label="账号状态">
        <template v-slot="{ row }">
          {{ row.status === 1 ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="change(row.id)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link
              slot="reference"
              :type="row.status === 0 ? 'success' : 'danger'"
            >
              {{ row.status === 0 ? "启用" : "禁用" }}
            </el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getEmployees, updateEmployee } from "@/api/employee";
export default {
  data() {
    return {
      input: "",
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getEmployees({ ...this.pageInfo, name: this.input }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleChangeStatus(row) {
      updateEmployee({ id: row.id, status: +!row.status }).then((res) => {
        this.$message.success("改变状态成功");
        this.init();
      });
    },
    add() {
      this.$router.push({ path: "/employee/add", query: { isEdit: "0" } });
    },
    change(id) {
      this.$router.push({
        path: `/employee/change`,
        query: { isEdit: "1", id },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 30px;
  .header {
    display: flex;
    justify-content: space-between;
    .btn {
      background-color: #ffc200;
      color: #333333;
    }
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
  }
  .el-table {
    margin-top: 20px;
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
